package com.kd.d5000acquire.util;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.Socket;
import java.util.*;
import java.util.Map.Entry;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class SendData {
    public static int sendAllInfo(String type, String time, String deviceid, LinkedHashMap<String, ArrayList<String>> map, String ip, int port) {
        ArrayList temp_velue_list = null;
        int time_count = 0;
        int i = 0;

        StringBuilder valueBuilder = new StringBuilder();
        Document doc = DocumentHelper.createDocument();
        Element rootEle = doc.addElement("lists");
        Element noEle = rootEle.addElement("list");
        noEle.setAttributeValue("device", deviceid);
        noEle.setAttributeValue("type", type);
        if ((null != map) && (!map.isEmpty())) {
            Set set = map.entrySet();
            Iterator iter = set.iterator();
            while (iter.hasNext()) {
                valueBuilder.delete(0, valueBuilder.length());
                Entry entry = (Entry)iter.next();
                Element ele = noEle.addElement((String)entry.getKey());
                temp_velue_list = (ArrayList)entry.getValue();
                time_count = temp_velue_list.size();
                for (int j = 0; j < temp_velue_list.size(); j++)
                {
                    valueBuilder.append((String)temp_velue_list.get(j));
                    if (j < temp_velue_list.size() - 1)
                    {
                        valueBuilder.append(",");
                    }
                }
                String value_str = valueBuilder.toString();

                ele.setText(value_str);
            }
            Element ti_ele = noEle.addElement("time");
            time_count--;
            valueBuilder.delete(0, valueBuilder.length());
            for (i = 0; i < time_count; i++)
            {
                valueBuilder.append(time);
                valueBuilder.append(",");
            }
            valueBuilder.append(time);
            String value_str = valueBuilder.toString();
            ti_ele.setText(value_str);
        } else {
            System.out.println("send message is null return!");
            return -1;
        }
        String value_str;
        StringWriter sw = new StringWriter();
        OutputFormat format = new OutputFormat();
        format.setIndentSize(0);
        format.setNewlines(true);
        format.setTrimText(true);
        format.setPadText(true);
        format.setNewLineAfterDeclaration(false);
        format.setEncoding("UTF-8");
        try {
            XMLWriter xmlWriter = new XMLWriter(sw, format);
            xmlWriter.write(doc);
        } catch (IOException localIOException2) {
        }
        finally {
            try {
                sw.close();
            } catch (IOException localIOException4) {
            }
        }
        System.out.println(sw.toString());
        System.out.println(new StringBuilder().append(ip).append("  ").append(port).toString());
        try
        {
            Socket socket = new Socket(ip, port);

            BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
            PrintWriter printWriter = new PrintWriter(bf);

            printWriter.println(sw.toString());
            printWriter.flush();
            socket.shutdownOutput();
            printWriter.close();

            socket.close();
        }
        catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
        Socket socket;
        return 0;
    }

    public static int sendAlarmData(String type, String time, String deviceid, List<String> list, String alarm_definition, String ip, int port) {
        Document doc = DocumentHelper.createDocument();
        if ((null != list) && (!list.isEmpty()))
        {
            Element rootEle = doc.addElement("type");

            rootEle.setAttributeValue("id", type);
            Element acoEle = rootEle.addElement("action");
            acoEle.setText("alarm");
            Element deoEle = rootEle.addElement("device_id");
            deoEle.setText(deviceid);
            Element tioEle = rootEle.addElement("time");
            tioEle.setText(time);
            Element defoEle = rootEle.addElement("definition_id");
            defoEle.setText(alarm_definition);
            Element daEle = rootEle.addElement("data");
            for (int i = 0; i < list.size(); i++) {
                daEle.setText((String)list.get(i));
            }
        }
        else
        {
            System.out.println("send AlarmMessage is null return!");
            return -1;
        }

        StringWriter sw = new StringWriter();

        OutputFormat format = new OutputFormat();
        format.setIndentSize(0);
        format.setNewlines(true);
        format.setTrimText(true);
        format.setPadText(true);
        format.setNewLineAfterDeclaration(false);
        format.setEncoding("UTF-8");
        try {
            XMLWriter xmlWriter = new XMLWriter(sw, format);
            xmlWriter.write(doc);
        } catch (IOException localIOException2) {
        }
        finally {
            try {
                sw.close();
            }
            catch (IOException localIOException4)
            {
            }
        }
        System.out.println(sw.toString());
        System.out.println(new StringBuilder().append(ip).append("  ").append(port).toString());
        try
        {
            Socket socket = new Socket(ip, port);

            BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
            PrintWriter printWriter = new PrintWriter(bf);

            printWriter.println(sw.toString());
            printWriter.flush();
            socket.shutdownOutput();
            printWriter.close();

            socket.close();
        }
        catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
        Socket socket;
        return 0;
    }
    public static int sendAlarmData(String type, String time, String deviceid, String devicename, List<String> list, String alarm_definition, String ip, int port) {
        Document doc = DocumentHelper.createDocument();
        if ((null != list) && (!list.isEmpty()))
        {
            Element rootEle = doc.addElement("type");

            rootEle.setAttributeValue("id", type);
            Element acoEle = rootEle.addElement("action");
            acoEle.setText("alarm");
            Element deoEle = rootEle.addElement("device_id");
            deoEle.setText(deviceid);
            Element dnameoEle = rootEle.addElement("nodename");
            dnameoEle.setText(devicename);
            Element tioEle = rootEle.addElement("time");
            tioEle.setText(time);
            Element defoEle = rootEle.addElement("definition_id");
            defoEle.setText(alarm_definition);
            Element daEle = rootEle.addElement("data");
            for (int i = 0; i < list.size(); i++) {
                daEle.setText((String)list.get(i));
            }
        }
        else
        {
            System.out.println("send AlarmMessage is null return!");
            return -1;
        }
        StringWriter sw = new StringWriter();

        OutputFormat format = new OutputFormat();
        format.setIndentSize(0);
        format.setNewlines(true);
        format.setTrimText(true);
        format.setPadText(true);
        format.setNewLineAfterDeclaration(false);
        format.setEncoding("UTF-8");
        try {
            XMLWriter xmlWriter = new XMLWriter(sw, format);
            xmlWriter.write(doc);
        } catch (IOException localIOException2) {
        }
        finally {
            try {
                sw.close();
            }
            catch (IOException localIOException4)
            {
            }
        }
        System.out.println(sw.toString());
        System.out.println(new StringBuilder().append(ip).append("  ").append(port).toString());
        try
        {
            Socket socket = new Socket(ip, port);

            BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
            PrintWriter printWriter = new PrintWriter(bf);

            printWriter.println(sw.toString());
            printWriter.flush();
            socket.shutdownOutput();
            printWriter.close();

            socket.close();
        }
        catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
        Socket socket;
        return 0;
    }

    public static int sendDisAlarmData(String type, String start_time, String end_time, String deviceid, List<String> list, String alarm_definition, String ip, int port) {
        Document doc = DocumentHelper.createDocument();
        if ((null != list) && (!list.isEmpty()))
        {
            Element rootEle = doc.addElement("type");
            rootEle.setAttributeValue("id", type);
            Element acoEle = rootEle.addElement("action");
            acoEle.setText("recover");
            Element deoEle = rootEle.addElement("device_id");
            deoEle.setText(deviceid);
            Element startoEle = rootEle.addElement("time");
            startoEle.setText(start_time);
            Element endoEle = rootEle.addElement("endtime");
            endoEle.setText(end_time);
            Element defoEle = rootEle.addElement("definition_id");
            defoEle.setText(alarm_definition);
            Element daEle = rootEle.addElement("data");
            for (int i = 0; i < list.size(); i++) {
                daEle.setText((String)list.get(i));
            }
        }
        else
        {
            System.out.println("send RecoverMessage is null return!");
            return -1;
        }
        StringWriter sw = new StringWriter();

        OutputFormat format = new OutputFormat();
        format.setIndentSize(0);
        format.setNewlines(true);
        format.setTrimText(true);
        format.setPadText(true);
        format.setNewLineAfterDeclaration(false);
        format.setEncoding("UTF-8");
        try {
            XMLWriter xmlWriter = new XMLWriter(sw, format);
            xmlWriter.write(doc);
        } catch (IOException localIOException2) {
        }
        finally {
            try {
                sw.close();
            }
            catch (IOException localIOException4) {
            }
        }
        System.out.println(sw.toString());
        System.out.println(new StringBuilder().append(ip).append("  ").append(port).toString());
        try
        {
            Socket socket = new Socket(ip, port);

            BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
            PrintWriter printWriter = new PrintWriter(bf);

            printWriter.println(sw.toString());
            printWriter.flush();
            socket.shutdownOutput();
            printWriter.close();

            socket.close();
        }
        catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
        Socket socket;
        return 0;
    }
    public static int sendDisAlarmData(String type, String start_time, String end_time, String deviceid, String devicename, List<String> list, String alarm_definition, String ip, int port) {
        Document doc = DocumentHelper.createDocument();
        if ((null != list) && (!list.isEmpty()))
        {
            Element rootEle = doc.addElement("type");
            rootEle.setAttributeValue("id", type);
            Element acoEle = rootEle.addElement("action");
            acoEle.setText("recover");
            Element deoEle = rootEle.addElement("device_id");
            deoEle.setText(deviceid);
            Element denameoEle = rootEle.addElement("nodename");
            denameoEle.setText(devicename);
            Element startoEle = rootEle.addElement("time");
            startoEle.setText(start_time);
            Element endoEle = rootEle.addElement("endtime");
            endoEle.setText(end_time);
            Element defoEle = rootEle.addElement("definition_id");
            defoEle.setText(alarm_definition);
            Element daEle = rootEle.addElement("data");
            for (int i = 0; i < list.size(); i++) {
                daEle.setText((String)list.get(i));
            }
        }
        else
        {
            System.out.println("send RecoverMessage is null return!");
            return -1;
        }
        StringWriter sw = new StringWriter();

        OutputFormat format = new OutputFormat();
        format.setIndentSize(0);
        format.setNewlines(true);
        format.setTrimText(true);
        format.setPadText(true);
        format.setNewLineAfterDeclaration(false);
        format.setEncoding("UTF-8");
        try {
            XMLWriter xmlWriter = new XMLWriter(sw, format);
            xmlWriter.write(doc);
        } catch (IOException localIOException2) {
        }
        finally {
            try {
                sw.close();
            }
            catch (IOException localIOException4) {
            }
        }
        System.out.println(sw.toString());
        System.out.println(new StringBuilder().append(ip).append("  ").append(port).toString());
        try
        {
            Socket socket = new Socket(ip, port);

            BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
            PrintWriter printWriter = new PrintWriter(bf);

            printWriter.println(sw.toString());
            printWriter.flush();
            socket.shutdownOutput();
            printWriter.close();

            socket.close();
        }
        catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
        Socket socket;
        return 0;
    }

    public static int sendNoticeAlarmData(String type, String time, String deviceid, List<String> list, String alarm_definition, String ip, int port) {
        Document doc = DocumentHelper.createDocument();
        if ((null != list) && (!list.isEmpty()))
        {
            Element rootEle = doc.addElement("type");
            rootEle.setAttributeValue("id", type);
            Element acoEle = rootEle.addElement("action");
            acoEle.setText("notice");
            Element deoEle = rootEle.addElement("device_id");
            deoEle.setText(deviceid);

            Element tioEle = rootEle.addElement("time");
            tioEle.setText(time);

            Element defoEle = rootEle.addElement("definition_id");
            defoEle.setText(alarm_definition);
            Element daEle = rootEle.addElement("data");
            for (int i = 0; i < list.size(); i++) {
                daEle.setText((String)list.get(i));
            }
        }
        else
        {
            System.out.println("send RecoverMessage is null return!");
            return -1;
        }
        StringWriter sw = new StringWriter();

        OutputFormat format = new OutputFormat();
        format.setIndentSize(0);
        format.setNewlines(true);
        format.setTrimText(true);
        format.setPadText(true);
        format.setNewLineAfterDeclaration(false);
        format.setEncoding("UTF-8");
        try {
            XMLWriter xmlWriter = new XMLWriter(sw, format);
            xmlWriter.write(doc);
        } catch (IOException localIOException2) {
        }
        finally {
            try {
                sw.close();
            }
            catch (IOException localIOException4) {
            }
        }
        System.out.println(sw.toString());
        System.out.println(new StringBuilder().append(ip).append("  ").append(port).toString());
        try
        {
            Socket socket = new Socket(ip, port);

            BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
            PrintWriter printWriter = new PrintWriter(bf);

            printWriter.println(sw.toString());
            printWriter.flush();
            socket.shutdownOutput();
            printWriter.close();

            socket.close();
        }
        catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
        Socket socket;
        return 0;
    }

    public static int sendNoticeAlarmData(String type, String time, String deviceid, String devicename, List<String> list, String alarm_definition, String ip, int port) {
        Document doc = DocumentHelper.createDocument();
        if ((null != list) && (!list.isEmpty()))
        {
            Element rootEle = doc.addElement("type");
            rootEle.setAttributeValue("id", type);
            Element acoEle = rootEle.addElement("action");
            acoEle.setText("notice");
            Element deoEle = rootEle.addElement("device_id");
            deoEle.setText(deviceid);
            Element denameoEle = rootEle.addElement("nodename");
            denameoEle.setText(devicename);
            Element tioEle = rootEle.addElement("time");
            tioEle.setText(time);

            Element defoEle = rootEle.addElement("definition_id");
            defoEle.setText(alarm_definition);
            Element daEle = rootEle.addElement("data");
            for (int i = 0; i < list.size(); i++) {
                daEle.setText((String)list.get(i));
            }
        }
        else
        {
            System.out.println("send RecoverMessage is null return !");
            return -1;
        }
        StringWriter sw = new StringWriter();

        OutputFormat format = new OutputFormat();
        format.setIndentSize(0);
        format.setNewlines(true);
        format.setTrimText(true);
        format.setPadText(true);
        format.setNewLineAfterDeclaration(false);
        format.setEncoding("UTF-8");
        try {
            XMLWriter xmlWriter = new XMLWriter(sw, format);
            xmlWriter.write(doc);
        } catch (IOException localIOException2) {
        }
        finally {
            try {
                sw.close();
            }
            catch (IOException localIOException4) {
            }
        }
        System.out.println(sw.toString());
        System.out.println(new StringBuilder().append(ip).append("  ").append(port).toString());
        try
        {
            Socket socket = new Socket(ip, port);

            BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
            PrintWriter printWriter = new PrintWriter(bf);

            printWriter.println(sw.toString());
            printWriter.flush();
            socket.shutdownOutput();
            printWriter.close();

            socket.close();
        }
        catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
        Socket socket;
        return 0;
    }

    public static int sendDStartAlarmData(String type, String time, String deviceid, String alarm_definition, String ip, int port) {
        Document doc = DocumentHelper.createDocument();
        if ((null != alarm_definition) && (null != deviceid))
        {
            Element rootEle = doc.addElement("type");
            rootEle.setAttributeValue("id", type);
            Element acoEle = rootEle.addElement("action");
            acoEle.setText("recover");
            Element deoEle = rootEle.addElement("device_id");
            deoEle.setText(deviceid);

            Element defoEle = rootEle.addElement("definition_id");
            defoEle.setText(alarm_definition);
            Element tioEle = rootEle.addElement("endtime");
            tioEle.setText(time);
        }
        else
        {
            System.out.println("send sendDStartAlarmData is null return!");
            return -1;
        }
        StringWriter sw = new StringWriter();

        OutputFormat format = new OutputFormat();
        format.setIndentSize(0);
        format.setNewlines(true);
        format.setTrimText(true);
        format.setPadText(true);
        format.setNewLineAfterDeclaration(false);
        format.setEncoding("UTF-8");
        try {
            XMLWriter xmlWriter = new XMLWriter(sw, format);
            xmlWriter.write(doc);
        } catch (IOException localIOException2) {
        }
        finally {
            try {
                sw.close();
            }
            catch (IOException localIOException4) {
            }
        }
        System.out.println(sw.toString());
        System.out.println(new StringBuilder().append(ip).append("  ").append(port).toString());
        try
        {
            Socket socket = new Socket(ip, port);

            BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
            PrintWriter printWriter = new PrintWriter(bf);

            printWriter.println(sw.toString());
            printWriter.flush();
            socket.shutdownOutput();
            printWriter.close();

            socket.close();
        }
        catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
        Socket socket;
        return 0;
    }
}